package com.starinside.wp.repository;

import com.starinside.wp.domain.liveRecord.RecordChildVo;
import com.starinside.wp.domain.liveRecord.RecordListVo;
import com.starinside.wp.domain.record.LiveRecord;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class LiveRecordNativeRepository extends BaseNativeSqlRepository{

    /**
     * @param childrenId
     */
    public List<LiveRecord> findLiveRecordByChildrenId(@Param("childrenId") String childrenId, Integer pageNum, Integer pageSize){
        String sql = "SELECT title,content,category,resourceUrl,thumbUrl,created,mediaType,operatorId, operatorName,headImgUrl FROM live_record where areaId =  (\n" +
                            "\tselect aid from children c where c.id = '%s' \n" +
                        ") order by created desc limit  %d,%d";

        sql =  String.format(sql, childrenId, pageNum * pageSize, pageSize);

        List<LiveRecord> items = sqlObjectList(sql, LiveRecord.class);

        return items;
    }

//    public List<LiveRecord> findLiveRecordByChildrenIds(String[] childrenIds, Integer pageNum, Integer pageSize){
//
//        if (childrenIds == null || childrenIds.length == 0){
//            return null;
//        }
//
//        String childrenIdsStr = "'" + StringUtils.join(childrenIds, "\',\'") + "'";
//
//        String sql = "SELECT title,content,category,resourceUrl,thumbUrl,created,mediaType,operatorId, operatorName,headImgUrl " +
//                "FROM live_record where  id in (" +
//                "SELECT liveRecordId from rel_children_live_record where childrenId in (%s)" +
//                ") order by created desc limit  %d,%d";
//
//        sql =  String.format(sql, childrenIdsStr, pageNum * pageSize, pageSize);
//
//        List<LiveRecord> items = sqlObjectList(sql, LiveRecord.class);
//
//        return items;
//    }

//    private String title;
//    private String content;
//    private Integer category;
//    private List resourceUrls;
//    private String thumbUrl;
//    private String created;
//    private Integer mediaType ;
//    private String teacherId;
//    private String teacherName;
//    private String headImgUrl ;

    public List<RecordListVo> findLiveRecordByChildrenIdBeforeTime(String childrenId, String beforeTime, Integer pageSize){
        String sql = "SELECT id,title,content,category,resourceUrl,thumbUrl,created,mediaType,operatorId as teacherId,operatorName as teacherName,headImgUrl FROM live_record where areaId =  (\n" +
                "\tselect aid from children c where c.id = '%s' \n" +
                ") and created<'%s' order by created desc limit  %s";

        sql =  String.format(sql, childrenId, beforeTime, pageSize);

        List<RecordListVo> items = sqlObjectList(sql, RecordListVo.class);

        return items;
    }


    public Long countLiveRecordByChildrenId(@Param("childrenId") String childrenId){
        String sql = "SELECT count(*) FROM live_record where areaId = (select aid from children c where c.id = '%s') ";

        sql =  String.format(sql, childrenId);

        Long ret = sqlSingleResult(sql);

        return ret;
    }

    public Long countLiveRecordByChildrenIds(String[] childrenIds){

        if (childrenIds == null || childrenIds.length == 0 ){
            return 0L;
        }

        String childrenIdsStr = "'" + StringUtils.join(childrenIds, "\',\'") + "'";

        String sql = "SELECT count(*) FROM live_record where id in (SELECT liveRecordId from rel_children_live_record where childrenId in (%s))";

        sql =  String.format(sql, childrenIdsStr);

        Long ret = sqlSingleResult(sql);

        return ret;
    }
}
